#include <jni.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <unordered_map>
#include "safeKeyboardLib.h"
#include "aes.h"
#include "stringUtil.h"

using namespace std;

unordered_map<string, string> hashmap;

// AES加密密钥，随机生成
static string aesSecretKey;

// 加密的iv，随机生成
static string aesSecretIV;

// 生成随机密钥
string generateSecretKey() {
    // 初始化为16位长度的空字符串
    std::string Password(16, '\0');
    // 设置随机数种子
    srand(static_cast<unsigned>(time(nullptr)));
    // 随机密钥包含的内容
    const char SECRET_CHARS[] = "01234abcdefghijklMNOPQRSTUVWXYZ_";
    for (int i = 0; i < 16; ++i) {
        int random = rand() % strlen(SECRET_CHARS);
        Password[i] = SECRET_CHARS[random];
    }
    return Password;
}

// 生成随机向量
string generateSecretIV() {
    // 初始化为16位长度的空字符串
    std::string Password(16, '\0');
    // 设置随机数种子
    srand(static_cast<unsigned>(time(nullptr)));
    // 随机密钥包含的内容
    const char IV_CHARS[] = "56789mnopqrstuvwxyzABCDEFGHIJKL_";
    for (int i = 0; i < 16; ++i) {
        int random = rand() % strlen(IV_CHARS);
        Password[i] = IV_CHARS[random];
    }
    return Password;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_generateSecretKey(JNIEnv *env, jclass type) {
    // 生成随机密钥和向量，保存到全局变量
    aesSecretKey = generateSecretKey();
    LOGD("########## generateSecretKey aesSecretKey = %s", aesSecretKey.c_str());
    aesSecretIV = generateSecretIV();
    LOGD("########## generateSecretKey aesSecretIV = %s", aesSecretIV.c_str());
}

extern "C"
JNIEXPORT jbyteArray
JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_encryption(JNIEnv *env, jclass type, jbyteArray srcData
) {
    return encryption(env, srcData, aesSecretKey, aesSecretIV);
}

extern "C"
JNIEXPORT jbyteArray
JNICALL Java_com_surfin_safe_keyboard_JNIMethod_decryption(JNIEnv *env, jclass
type,
                                                           jbyteArray srcData
) {
    return
            decryption(env, srcData, aesSecretKey, aesSecretIV
            );
}

extern "C"
JNIEXPORT void JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_addKey(JNIEnv
                                               *env,
                                               jclass type, jstring
                                               id_,
                                               jstring text_
) {
    string input = jstring2str(env, text_);
    LOGD("########## addKey input = %s", input.c_str());
    hashmap[
            jstring2str(env, id_
            )] =
            hashmap[
                    jstring2str(env, id_
                    )] +
            input;
}

extern "C"
JNIEXPORT jstring
JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_getKey(JNIEnv *env, jclass
type,
                                               jstring id_
) {
    string data = hashmap[jstring2str(env, id_)];
    if (data.

            c_str()

        == NULL) {
// 没有加密数据，返回空的字符串
        return env->NewStringUTF("");
    }
    return env->
            NewStringUTF(data
                                 .

                                         c_str()

    );
}


extern "C"
JNIEXPORT void JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_deleteKey(JNIEnv *env, jclass type, jstring id_) {
    string input = hashmap[jstring2str(env, id_)];
    input.pop_back();
    hashmap[jstring2str(env, id_)] = input;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_clearKey(JNIEnv *env, jclass type, jstring id_) {
    string input = hashmap[jstring2str(env, id_)];
    LOGD("########## clearKey input = %s", input.c_str());
    input.clear();
    hashmap[jstring2str(env, id_)] = input;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_surfin_safe_keyboard_JNIMethod_releaseKey(JNIEnv *env, jclass type, jstring id_) {
    const char *id = env->GetStringUTFChars(id_, 0);
    hashmap.erase(id);
    env->ReleaseStringUTFChars(id_, id);
}